package com.yc.hotel.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yc.hotel.mapper.IOrderInfoMapper;
import com.yc.hotel.room.feign.IRoomInfoFeignClient;
import com.yc.hotel.dto.RoomInfoDTO;
import com.yc.hotel.entity.OrderInfo;
import com.yc.hotel.dto.OrderInfoDTO;
import com.yc.hotel.service.IOrderInfoService;

@Service
public class OrderInfoServiceImpl implements IOrderInfoService{
	@Autowired
	private IOrderInfoMapper orderInfoMapper;
	
	@Autowired
	private IRoomInfoFeignClient roomInfoMapper;
	
	@Override
	public int add(String rno, String uno, String tno, String ino) {
		String ono = UUID.randomUUID().toString();//生成订单编号
		RoomInfoDTO roomInfo = roomInfoMapper.findByRno(Integer.parseInt(rno));
		
		OrderInfo order = new OrderInfo();
		order.setOno(ono);
		order.setRno(Integer.parseInt(rno));
		order.setUno(Integer.parseInt(uno));
		order.setIno(Integer.parseInt(ino));
		Double totalPrice = roomInfo.getPrice();
		if(tno.equals("101")) {
			order.setTotalPrice(totalPrice);
		}else {
			order.setTotalPrice(totalPrice*3);
		}
		
		
		int result = orderInfoMapper.add(order);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rno", rno);
		map.put("status", 2);
		roomInfoMapper.updateStatus(map);
		return result;
	}

	@Override
	public int update(String ono, Integer status) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ono", ono);
		map.put("status", status);
		System.out.println(map+"update");
		return orderInfoMapper.update(map);
	}

	@Override
	public int update(Map<String, Object> map) {
		return orderInfoMapper.update(map);
	}
	
	@Override
	public int updatePrice(Map<String, Object> map) {
		return orderInfoMapper.updatePrice(map);
	}

	@Override
	public List<OrderInfo> finds(Map<String, Object> map) {
		return orderInfoMapper.finds(map);
	}
	
	@Override
	public List<OrderInfoDTO> findRoom(Map<String, Object> map) {
		return orderInfoMapper.findRoom(map);
	}

	@Override//退房
	public int outOrder(String ono, String price) {
		this.update(ono, 5);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ono", ono);
		int days = this.days(map);
		OrderInfo order = orderInfoMapper.findByOno(ono);
		int rno = order.getRno();
		
		double totalPrice = Double.parseDouble(price);
		if(days >= 3) {
			totalPrice = totalPrice * (days-3);
		}else {
			totalPrice = 0;
			this.endRent(map);
			Map<String, Object> roomMap = new HashMap<String, Object>();
			roomMap.put("status", 1);
			roomMap.put("rno",rno);
			roomInfoMapper.updateStatus(roomMap );
			return 1;
		}
		map.put("totalPrice", totalPrice);
		this.updatePrice(map);
		String nono = UUID.randomUUID().toString();//生成订单编号
		map = new HashMap<String, Object>();
		map.put("ono", ono);
		map.put("nono", nono);
		orderInfoMapper.newOno(map);
		return 1;
	}

	@Override
	public int days(Map<String, Object> map) {
		
		return orderInfoMapper.days(map);
	}
	
	@Override
	public int endRent(Map<String, Object> map) {
		
		return orderInfoMapper.endRent(map);
	}

	@Override
	public int endSale(Map<String, Object> map) {
		map.put("status", 3);
		int result = orderInfoMapper.update(map);
		String rno = map.get("rno") +"";
		RoomInfoDTO room = roomInfoMapper.findByRno(Integer.parseInt(rno));
		Map<String, Object> change = new HashMap<String, Object>();
		change.put("status", 0);
		change.put("rno", rno);
		roomInfoMapper.updateStatus(change);
		int uno = Integer.parseInt(map.get("uno") + "");
		room.setUno(uno);
		
		roomInfoMapper.endSale(room);
		return result;
		
	}

}
